<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Jypage
{
	var $table;

	var $key;

	var $where;

	var $field;

	var $condition;

	var $page;

	var $pagesize;

	var $pagecount;

	var $recordcount;
	
	var $jydb;
	
	var $pageurl;
	
	var $lang=array();

    function __construct()
    {
    	self::Jypage();
    }
    
    function SetDb($db)
    {
    	$this->jydb = $db;
    }
    
	function SetLang($page_record_lang="")
	{
		$this->lang['page_TableError'] = "数据表名不能为空";
		$this->lang['page_KeyError'] = "数据表主键名不能为空";
		$this->lang['page_error'] = "函数条件错误,数据表名,主键名不能为空,当前页为空或非法,字段需为一个数组或空";
		$this->lang['page_sum'] = "共";
		$this->lang['page_record'] = empty($v['page_record_lang'])?'条记录':$page_record_lang;
		$this->lang['page_pages'] = "页";
		$this->lang['page_fristpage'] = "首页";
		$this->lang['page_lastpage'] = "末页";
		$this->lang['page_precpage'] = "上一页";
		$this->lang['page_nextpage'] = "下一页";
		$this->lang['page_jumptopage'] = "转到第";
	}
	
    function SetAttr($v)
    {
	 	$this->table = empty($v['table'])?$this->msg($this->lang['page_TableError']):$v['table'];

		$this->key = empty($v['key'])?$this->msg($this->lang['page_KeyError']):$v['key'];

		$this->where = empty($v['where'])?1:$v['where'];
		
		$this->pageurl = empty($v['pageurl'])?"":$v['pageurl'];

		$this->field = empty($v['field'])?"*":implode(",",$v['field']);

		$this->condition = empty($v['condition'])?" ORDER BY ".$v['key']." DESC":$v['condition'];//数据排列条件;

		$this->page = intval(empty($v['page'])?1:$v['page']);

		$this->pagesize = $v['pagesize']==""?10:$v['pagesize'];
    }

    function Jypage()
	{
        $this->SetLang();
	}

	function pages()
	{  
		$SQL = "SELECT COUNT(".$this->key.") AS total FROM ".$this->table." WHERE ".$this->where."";
		
		$query = $this->jydb->query($SQL);
			
		$rs = $query->row_array(); // 分页返回值
			
		$pages = array("ZeroPage"=>array());
	
	    if(count($rs)>0)
	    {
	    	$recordcount = $rs['total'];//总记录集

			$pagecount = ceil($recordcount/$this->pagesize);//总页数
	
			$this->page = $this->page>$pagecount?$pagecount:$this->page;
	
			$this->page= $this->page<1?1:$this->page;
	
			$start = ($this->page-1)*$this->pagesize;
	
		    $SQL = "SELECT ".$this->field." FROM ".$this->table." WHERE ".$this->where.$this->condition." LIMIT ".$start.",".$this->pagesize."";
			
			$query = $this->jydb->query($SQL);
	
			$this->recordcount = $recordcount;
	
			$this->pagecount = $pagecount;
	
			$pages['JPage'] =  $query->result_array();
			
	    }
		return $pages;
	}
	
	function SetPageStrJy($page = 10, $simple = 0, $onclick = '')
	{
			$multipage = '';
			
			if($this->recordcount > $this->pagesize) {
				$offset = 2;

				if($page > $this->pagecount) {
					$from = 1;
					$to = $this->pagecount;
				} else {
					$from = $this->page - $offset;
					$to = $from + $page - 1;
					if($from < 1) {
						$to = $this->page + 1 - $from;
						$from = 1;
						if($to - $from < $page) {
							$to = $page;
						}
					} elseif($to > $this->pagecount) {
						$from = $this->pagecount - $page + 1;
						$to = $this->pagecount;
					}
				}

				$multipage = ($this->page - $offset > 1 && $this->pagecount > $page ? '<a href="'.$this->pageurl."/".(1).'">&lsaquo;</a>' : '').
					($this->page > 1 && !$simple ? '<a href="'.$this->pageurl."/".($this->page-1).'">上一页</a>&nbsp;' : '');
				for($i = $from; $i <= $to; $i++) {
					$multipage .= $i == $this->page ?$i.'&nbsp;' :
						'<a href="'.$this->pageurl."/".$i.'">'.$i.'</a>&nbsp;';
				}

				$multipage .= ($this->page < $this->pagecount && !$simple ? '<a href="'.$this->pageurl."/".($this->page+1).'">下一页</a>' : '').
					($to < $this->pagecount ? '<a href="'.$this->pageurl."/".($this->pagecount).'">&rsaquo;</a>' : '');

				$multipage = $multipage ? (!$simple ? $this->page.'/'.$this->pagecount.''.$this->lang['page_pages']."&nbsp;&nbsp;": '').$multipage: '';
				$multipage.='';
			}
			return $multipage;
	}
	
    function SetPageStr()
	{	
        $pageurl = "";    
        $PageStr = "";
		$PageStr .= $this->lang['page_sum']."<strong>".$this->recordcount."</strong>".$this->lang['page_record']."&nbsp;&nbsp;".$this->lang['page_sum']."<strong>".$this->pagecount."</strong>".$this->lang['page_pages']."&nbsp;&nbsp;".$this->page."/".$this->pagecount."&nbsp;&nbsp;";

		$PageStr .= $this->page !=1?"<a href='".$this->pageurl."1'>".$this->lang['page_fristpage']."</a>":$this->lang['page_fristpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page>1?"<a href='".$this->pageurl.($this->page-1)."'>".$this->lang['page_precpage']."</a>":$this->lang['page_precpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page<$this->pagecount?"<a href='".$this->pageurl.($this->page+1)."'>".$this->lang['page_nextpage']."</a>":$this->lang['page_nextpage'];

		$PageStr .= "&nbsp;&nbsp;";

		$PageStr .= $this->page!=$this->pagecount?"<a href='".$this->pageurl.$this->pagecount."'>".$this->lang['page_lastpage']."</a>":$this->lang['page_lastpage'];

		return $PageStr;

	}
	
	function SetSimplePageStr()
	{
        $PageStr = array();
		
		$PageStr["prev"]= $this->page>1?$this->pageurl.($this->page-1):"javascript:alert('前面没有了!')";

		$PageStr["next"]= $this->page<$this->pagecount?$this->pageurl.($this->page+1):"javascript:alert('后面没有了!')";
        return $PageStr;
	}

	function AjaxSetPageStr($commendid="",$page = 5, $simple = 0, $onclick = '')
	{
			$multipage = '';

			//url处理
			$mpurl = $_SERVER['PHP_SELF'];
			if(isset($_SERVER['QUERY_STRING']))
			{
				$mpurl .= '?' . preg_replace_callback('/&page=[0-9]*/','',$_SERVER['QUERY_STRING']) . '&';
			}
			if($this->recordcount > $this->pagesize) {
				$offset = 2;
					$from = 1;
					$to = $this->pagecount;
				} else {
					$from = $this->page - $offset;



					$to = $from + $page - 1;
					if($from < 1) {
						$to = $this->page + 1 - $from;
						$from = 1;
						if($to - $from < $page) {
							$to = $page;
						}
					} elseif($to > $this->pagecount) {
						$from = $this->pagecount - $page + 1;
						$to = $this->pagecount;
					}
				}

				$multipage = ($this->page - $offset > 1 && $this->pagecount > $page ? '<a href="javascript:AjaxPage('.(1).')">&lsaquo;</a>' : '').
					($this->page > 1 && !$simple ? '<a href="javascript:AjaxPage('.$commendid.",".($this->page-1).')">上一页</a>' : '');
				for($i = $from; $i <= $to; $i++) {
					$multipage .= $i == $this->page ? '<SPAN>'.$i.'</span>' :
						'<a href="javascript:AjaxPage('.$commendid.",".$i.')">'.$i.'</a>';
				}

				$multipage .= ($this->page < $this->pagecount && !$simple ? '<a href="javascript:AjaxPage('.$commendid.",".($this->page+1).')">下一页</a>' : '').
					($to < $this->pagecount ? '<a href="javascript:AjaxPage('.($commendid.",".$this->pagecount).')">&rsaquo;</a>' : '');

				$multipage = $multipage ? '<span>'.(!$simple ? $this->page.'/'.$this->pagecount.''.$this->lang['page_pages'].'&nbsp;&nbsp;' : '').$multipage.'</span>' : '';
				$multipage.='';
			}
			return $multipage;
		}
}
?>
